package com.ncs.leisure.beans.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ncs.leisure.beans.business.BeansContactBusiness;
import com.ncs.leisure.beans.memory.MapList;
import com.ncs.leisure.overall.business.ContactBusiness;

public class FormatBeans implements ServletContextListener{

	public void contextInitialized(ServletContextEvent arg0) {
		
		try {
			
			BeansContactBusiness beansContact = (BeansContactBusiness) arg0.getServletContext().getAttribute("NCSLeisure_beans");
			
		 	if(beansContact==null||beansContact.equals("")){
		 		
		 		ApplicationContext ctx=null;
		 		
		 		beansContact=null;																									
		 	 	
				ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
			 
				beansContact = (BeansContactBusiness)ctx.getBean("beansContactBusiness");

				arg0.getServletContext().setAttribute("NCSLeisure_beans", beansContact);
				
			}
		 	
			System.out.println("加载NCSLeisureBeans初始化参数，成功！");
		 	
		}catch(Exception e) {
			
			System.out.println("加载NCSLeisureBeans初始化参数，失败！");
		
			e.printStackTrace();
		
		}
		
		MapList mapList=(MapList)arg0.getServletContext().getAttribute("BeansHall");
		
		if(mapList==null){
			
			mapList=new MapList();
			
			arg0.getServletContext().setAttribute("BeansHall",mapList);
			
		}
		
	}

	public void contextDestroyed(ServletContextEvent arg0) {
		
		arg0.getServletContext().removeAttribute("NCSLeisure_beans");
		
		System.out.println("加载NCSLeisureBeans参数注销，成功！");
		
		arg0.getServletContext().removeAttribute("BeansHall");
		
	}


}
